<!--
 * @Author: wangming
 * @Date: 2021-05-27 17:52:33
 * @LastEditors: wangming
 * @LastEditTime: 2022-03-22 09:54:17
 * @Description: 业务模型
-->
<template>
  <el-form-item label="绑定树菜单业务模型">
    <tree-select
      v-model="value.treeconfig.__config.modelCode"
      class="mb10"
      placeholder="请选择业务模型"
      filterable
      :fieldMap="{
        value: 'code',
        label: 'name',
        disabled: 'disabled',
      }"
      :filterNodeMethod="filterNodeMethod"
      :treeData="list"
      @change="changeModel"
    />
  </el-form-item>
</template>
<script>
import basicmixins from "./basicmixins";
import { getHmmodelfolderListTree } from "@/apis/paas/HmModelFolderController.js";
import { filterNodeMethod } from "@/utils/treeSelectFormat";
import lodash from "lodash";

export default {
  async mounted() {
    var data = await getHmmodelfolderListTree();
    data.forEach((node) => {
      node.type = "folder";
    });
    this.list = this.createModelTreeNode(data);
  },
  mixins: [basicmixins],
  data() {
    return {
      filterNodeMethod,
      list: [],
    };
  },
  methods: {
    changeModel(modelCode) {
      this.list.forEach((folder) => {
        const children = folder.modelResults || [];
        children.forEach((item) => {
          if (item.code === modelCode) {
            this.value.treeconfig.__config.moduleId = item.id;
          }
        });
      });
    },
    createModelTreeNode(tree) {
      return lodash.cloneDeep(tree).map((node) => {
        node.disabled = node.type === "folder";
        if (node.modelResults) {
          node.id = "folder" + node.id;
          node.children = node.modelResults || [];
          node.children.forEach((item) => {
            item.name = `${item.name}(${
              item.supportProcess ? "流程模型" : "表单模型"
            })(${item.type})`;
          });
        }
        return node;
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
